package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentTeacher;

@Controller
@RequestMapping("/student")
public class StudentController {

	@RequestMapping("/insert1")
	public ModelAndView insert1(Student student) {
		System.out.println("StudentController.insert()");
		System.out.println(student);
		
		ModelAndView modelAndView = new ModelAndView();
		// requst.setAttribute("student", student);
		modelAndView.addObject("student", student);
		// request.getRequestDispatcher("/student_info.jsp");
		modelAndView.setViewName("/student_info.jsp");
		return modelAndView;
	}
	
	@RequestMapping("/insert2")
	public String insert2(String name, Integer age, String gender) {
		System.out.println("StudentController.insert()");
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		System.out.println("gender: " + gender);
		
		return "/student_info.jsp";
	}
	
	//@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@RequestMapping(value = "/insert3", method = {RequestMethod.GET,RequestMethod.POST})
	public String insert3(Student student, Model model) {
		System.out.println("StudentController.insert()");
		System.out.println(student);
		
		// requst.setAttribute("student", student);
		model.addAttribute("student", student);
		// request.getRequestDispatcher("/student_info.jsp");
		return "/student_info.jsp";
	}
	
	@RequestMapping(value = "/insert", method = {RequestMethod.GET,RequestMethod.POST})
	public void insert(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);
		request.setAttribute("student", student);
		request.getRequestDispatcher("/student_info.jsp").forward(request, response);
		
	}
	
	@RequestMapping("/insertStudentTeacher")
	public void insertStudentTeacher(StudentTeacher studentTeacher) {
		System.out.println(studentTeacher);
	}
	
	// /07SpringMVC/student/deleteAll.action?ids=1&ids=2&ids=3
	// ids:1
	// ids:2
	// ids:3
	@RequestMapping("/deleteAll1")
	public void deleteAll1(HttpServletRequest request) {
		String[] ids = request.getParameterValues("ids");
		System.out.println(Arrays.toString(ids)); // [1, 2, 3]
	}
	
	@RequestMapping("/deleteAll")
	public void deleteAll(Integer[] ids) {
		System.out.println(Arrays.toString(ids)); // [1, 2, 3]
	}
	
	@RequestMapping("/selectAll")
	public String selectAll() {
		System.out.println("StudentController.selectAll()");
		// 转发到jsp页面
		return "/student_info.jsp";
	}
	
	@RequestMapping("/deleteById")
	public String deleteById(Integer id) {
		System.out.println("id: " + id);
		
		// 转发到另一个action请求
		// return "forward:/student/selectAll.action";
		// 重定向
		return "redirect:/student/selectAll.action";
	}
	
	@ResponseBody
	@RequestMapping("/selectById")
	public Student selectById(Integer id) {
		Student student = new Student(1, "张三", 23, "男");
		return student;
	}
	
	@ResponseBody
	@RequestMapping("/selectList")
	public List<Student> selectList() {
		Student student1 = new Student(1, "张三1", 23, "男");
		Student student2 = new Student(2, "张三2", 23, "男");
		Student student3 = new Student(3, "张三3", 23, "男");
		Student student4 = new Student(4, "张三4", 23, "男");
		List<Student> list = new ArrayList<Student>();
		list.add(student1);
		list.add(student2); 
		list.add(student3);
		list.add(student4);
		return list;
	}
	
	@RequestMapping("/selectByPage")
	public void selectByPage(@RequestParam(value = "page", defaultValue = "1")Integer pageNo, @RequestParam(required = true)Integer pageSize) {
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
	}
	
	@RequestMapping("/getStudentInsertPage")
	public String getStudentUpdatePage() {
		// return "/jsp/student_update.jsp";
		return "student_update";
	}
}
